﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace BtoBSWT315.User
{
    public partial class Add_cp : System.Web.UI.Page
    {

        private int Gsid;
        private int hybz;

        private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (Session["user"] == null)
            {
                Response.Redirect("../Default.aspx");
            }

            if (!IsPostBack)
            {
                EditDataBind();
            }
        }

        private void EditDataBind()
        {
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
            //string sql = "select * from qyml where [user]='" + Session["user"] + "'";
            string sql = "select name,qymc,address,sf,city,phone,fax,email,web,mobile from qyml where [user]='" + Session["user"] + "'";
            DataTable dataTable = null;
            dataTable = classdb.GetTables(sql);
            if (dataTable != null)
            {
                foreach (DataRow row in dataTable.Rows)
                {
                    lxr.Text = row["name"].ToString();                       //联系人
                    sccj.Text = row["qymc"].ToString();                     //公司名称
                    address.Text = row["address"].ToString();               //公司地址
                    sf.Text = row["sf"].ToString();                         //所在省份
                    city.Text = row["city"].ToString();                     //所在城市
                    phone.Text = row["phone"].ToString();                   //联系电话
                    fax.Text = row["fax"].ToString();                       //传真
                    email.Text = row["email"].ToString();                   //邮件
                    web.Text = row["web"].ToString();                       //传真
                    mobile.Text = row["mobile"].ToString();
                }
                dataTable = null;
            }

            //定义所需SQL语句
            string sql1 = "select * from class_1";
            //引入数据库操作类        
            dataTable = classdb.GetTables(sql1);
            if (dataTable != null)
            {
                BigClass.DataSource = dataTable.DefaultView;
                BigClass.DataTextField = "Sort";
                BigClass.DataValueField = "SortID";
                BigClass.DataBind();
                BigClass.SelectedIndex = 0;                   //行业大类
                dataTable = null;
            }

            //定义所需SQL语句
            string sql2 = "select * from class_2 where sortid=" + BigClass.SelectedValue;

            dataTable = classdb.GetTables(sql2);
            if (dataTable != null)
            {
                SmallClass.DataSource = dataTable.DefaultView;
                SmallClass.DataTextField = "TypeName";
                SmallClass.DataValueField = "TypeID";
                SmallClass.DataBind();
                SmallClass.SelectedIndex = 0;
            }
        }

        private void BigClass_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            //定义所需SQL语句
            string sql = "select * from class_2 where sortid=" + BigClass.SelectedValue;
            //引入数据库操作类
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();
            DataTable dataTable = null;
            dataTable = classdb.GetTables(sql);

            if (dataTable != null)
            {
                SmallClass.DataSource = dataTable.DefaultView;
                SmallClass.DataTextField = "TypeName";
                SmallClass.DataValueField = "TypeID";
                SmallClass.DataBind();                //行业小类
            }
        }
        private void Button1_Click(object sender, System.EventArgs e)
        {
            if (String.IsNullOrEmpty(cpmc.Text))
            {
                Response.Write("<script>alert('产品名称不能为空');</script>");
                Response.Write("<script>history.go(-1);</script>");
                Response.End();
            }

            //引入数据库操作类
            Admin.Admin_ClassDb classdb = new Admin.Admin_ClassDb();

            bool IsVIP = classdb.CheckVIP(Session["user"].ToString());

            //（1）添加是否是付费会员验证
            string checkCountSql = "select count(*) as [id] from [spzs] inner join qyml on spzs.gsid=qyml.id where qyml.user='" + Session["user"].ToString() + "'";

            if (!IsVIP && classdb.GetCount(checkCountSql) >= 10)
            {
                Response.Write("<script>alert('您已经上传了10张产品图片！如果您想上传更多产品照片，请先升级为VIP会员！');</script>");
                Response.Write("<script>history.go(-1);</script>");
                Response.End();
                return;
            }
            else
            {
                #region (2)先上传图片

                if (!String.IsNullOrEmpty(DefaultPicUrl.PostedFile.FileName))
                {
                    if (Admin.HtmlInputFileControl.IsAllowedExtension(DefaultPicUrl))
                    {
                        if (Admin.HtmlInputFileControl.IsAllowedLength(DefaultPicUrl))
                        {
                            Picture.Text = Admin.HtmlInputFileControl.SaveFile(DefaultPicUrl, "Products");
                        }
                        else
                        {
                            Response.Write("<script>alert('上传图片过大，请选择较小的图片上传！');</script>");
                            Response.Write("<script>history.go(-1);</script>");
                            Response.End();
                        }
                    }
                    else
                    {
                        Response.Write("<script>alert('只支持gif/jpg/jpeg/bmp/png');</script>");
                        Response.Write("<script>history.go(-1);</script>");
                        Response.End();
                    }
                }
                else
                {
                    Response.Write("<script>alert('请先选择上传的图片！');</script>");
                    Response.Write("<script>history.go(-1);</script>");
                    Response.End();
                }
                #endregion

                #region (3)存储数据到数据库


                //int ID=Convert.ToInt32(Request.QueryString["ID"]);
                ////定义所需SQL
                //string sql="update spzs set Cpmc='"+cpmc.Text+"',cpbh='"+cpbh.Text+"',cpsb='"+cpsb.Text+"',cpcd='"+cpcd.Text+"',cpgg='"+cpgg.Text+"',sortid='"+BigClass.SelectedValue+"',typeid='"+SmallClass.SelectedValue+"'";
                //sql+=",cpjg='"+cpjg.Text+"',Picture='"+Picture.Text+"',jysm='"+jysm.Text+"',xxsm='"+content.Text+"',lxr='"+lxr.Text+"',sccj='"+sccj.Text+"'";
                //sql+=",address='"+address.Text+"',postcode='"+postcode.Text+"',sf='"+sf.Text+"',city='"+city.Text+"',phone='"+phone.Text+"',fax='"+fax.Text+"'";
                //sql+=",email='"+email.Text+"',web='"+web.Text+"'";
                //sql+=" where ID="+ID;


                string sqlGsid = "select id,hybz from qyml where [user]='" + Session["user"] + "'";
                DataTable dataTable = null;

                dataTable = classdb.GetTables(sqlGsid);
                if (dataTable != null && dataTable.Rows.Count > 0)
                {
                    foreach (DataRow row in dataTable.Rows)
                    {
                        Gsid = Convert.ToInt32(row["id"].ToString());
                        hybz = Convert.ToInt32(row["hybz"].ToString());
                    }
                }

                //TODO;添加SPZS其他字段   GSID，hybz，idate为必填字段
                string insertSql = "insert into spzs (cpmc,cpbh,cpsb,cpcd,cpgg,cpjg,sortid,typeid,Picture,jysm,xxsm,lxr,sccj,address,sf,city,phone,fax,email,web,mobile,gsid,hybz,idate )values('" + cpmc.Text + "','" + cpbh.Text + "','" + cpsb.Text + "','" + cpcd.Text + "','" + cpgg.Text + "','" + cpjg.Text + "','" + BigClass.SelectedValue + "','" + SmallClass.SelectedValue + "','" + Picture.Text + "','" + jysm.Text + "','" + Add_cp_content.Text + "','" + lxr.Text + "','" + sccj.Text + "','" + address.Text + "','" + sf.Text + "','" + city.Text + "','" + phone.Text + "','" + fax.Text + "','" + email.Text + "','" + web.Text + "','" + mobile.Text + "','" + Gsid + "','" + hybz + "','" + DateTime.Now.ToShortDateString() + "')";

                if (classdb.GetNonResult(insertSql) != 0)
                {
                    cpmc.Text = "";
                    cpbh.Text = "";
                    cpcd.Text = "";
                    jysm.Text = "";
                    Response.Write("<script>alert('操作成功');</script>");
                    Response.Write("<script>history.go(-2);</script>");
                    Response.End();
                }
                else
                {
                    Response.Write("<script>alert('操作失败！请重新填写。');</script>");
                    Response.Write("<script>history.go(-1);</script>");
                    Response.End();
                }
                #endregion
            }
        }



        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.BigClass.SelectedIndexChanged += new System.EventHandler(this.BigClass_SelectedIndexChanged);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

    }
}